layui.use(['layer'], function () {

    var $ = layui.$;


    $(document).on('click', '.open-btn', function () {
        tree_open($(this));
    });

    $(document).on('click', '.open-child-btn', function () {
        row_clicked($(this));
    });

    // 展开收缩
    function tree_open(obj) {
        var tree = $('#list-table tr[id^="2_"], #list-table tr[id^="3_"], #list-table tr[id^="4_"], #list-table tr[id^="5_"], #list-table tr[id^="6_"]'); //'table-row'，支持6级
        if (tree.css('display') == 'table-row') {
            $(obj).html('<i class="layui-icon layui-icon-down layuiadmin-button-btn"></i>展开');
            tree.css('display', 'none');
            $("span[id^='icon_']").removeClass('layui-icon-triangle-d');
            $("span[id^='icon_']").addClass('layui-icon-triangle-r');
        } else {
            $(obj).html('<i class="layui-icon layui-icon-up layuiadmin-button-btn"></i>收缩');
            tree.css('display', 'table-row');
            $("span[id^='icon_']").addClass('layui-icon-triangle-d');
            $("span[id^='icon_']").removeClass('layui-icon-triangle-r');
        }
    }

    // 展开/收缩 下级
    function row_clicked(obj) {
        obj = obj[0]; // obj['context']
        var span = obj;
        obj = obj.parentNode.parentNode;
        var table = document.getElementById('list-table');
        var level = parseInt(obj.className);
        var cur_row = false;
        var sub_display = $(span).hasClass('layui-icon-triangle-d') ? 'none' : '' ? 'block' : 'table-row';
        //console.log(sub_display);
        if (sub_display == 'none') {
            $(span).removeClass('layui-icon-triangle-d');
            $(span).addClass('layui-icon-triangle-r');
        } else {
            $(span).removeClass('layui-icon-triangle-r');
            $(span).addClass('layui-icon-triangle-d');
        }

        for (var i = 0; i < table.rows.length; i++) {
            var row = table.rows[i];
            if (row == obj) {
                cur_row = true;
            } else {
                if (cur_row == true) {
                    var cur_level = parseInt(row.className);
                    var icon = 'icon_' + row.id;
                    if (cur_level > level) {
                        // 下级
                        row.style.display = sub_display;
                        var imgObj = document.getElementById(icon);
                        if (sub_display != 'none') {
                            $(imgObj).removeClass('layui-icon-triangle-r');
                            $(imgObj).addClass('layui-icon-triangle-d');
                        } else {
                            $(imgObj).removeClass('layui-icon-triangle-d');
                            $(imgObj).addClass('layui-icon-triangle-r');
                        }
                    } else {
                        // 同级
                        cur_row = false;
                        break;
                    }
                }
            }
        }

        for (var j = 0; j < obj.cells[0].childNodes.length; j++) {
            var imgObj = obj.cells[0].childNodes[j];
            if (imgObj.tagName == "IMG") {
                if ($(imgObj).hasClass('layui-icon-triangle-r')) {
                    $(imgObj).removeClass('layui-icon-triangle-r');
                    $(imgObj).addClass('layui-icon-triangle-d');
                } else {
                    $(imgObj).removeClass('layui-icon-triangle-d');
                    $(imgObj).addClass('layui-icon-triangle-r');
                }
            }
        }
    }
});